<?php
/**
 * Type.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-14 00:08:21
 * @modified    2023-02-14 00:08:21
 *
 */

namespace App\Models\Product;

use App\Models\BaseModel;
use App\Models\Product\Type\Article;
use App\Models\UserModel;
use App\Services\ImageService;

class Type extends BaseModel
{
    protected $table = 'product_type';

    protected $is_cache = false;

    protected $fillable = [
        'category_id',
        'type_number',
        'name',
        'image',
        'intro',
        'warranty_start_type',
        'warranty_cycle_count',
        'warranty_start_unit',
        'create_user_id'
    ];

    protected $appends = ['image_url'];

    public function getImageUrlAttribute()
    {
        return (new ImageService($this->image ?? ''))->getUrl();
    }

    public function scopeByNumber($query, $number)
    {
        return $query->where('type_number', $number);
    }

    public function scopeByName($query, $name)
    {
        return $query->where('name', $name);
    }

    public function boms()
    {
        return $this->hasMany(\App\Models\Product\Type\Bom::class, 'product_type_id', 'id');
    }

    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id', 'id');
    }

    public function articles()
    {
        return $this->hasMany(Article::class, 'product_type_id');
    }

    public function user()
    {
        return $this->belongsTo(UserModel::class, 'create_user_id');
    }
}